package com.yonyou.crm.sys.dataright.handler;

import com.yonyou.crm.sys.dataright.entity.RightActionEnum;
import com.yonyou.crm.sys.dataright.entity.RightConfig;
import com.yonyou.crm.sys.dataright.entity.RightObject;
import com.yonyou.crm.sys.dataright.entity.SuperVO;

public class ParticipantHandler implements RightHandler{

	//权限实体信息
	RightConfig rightConfig;
	//当前用户
	Long curUser;
	
	public ParticipantHandler(RightConfig rightConfig, Long curUser) {
		super();
		this.rightConfig = rightConfig;
		this.curUser = curUser;
	}

	@Override
	public boolean hasRight(SuperVO vo) {
		//参与人只有查看权限
		return rightConfig.getActionId() == RightActionEnum.VIEW.getValue();
	}

	@Override
	public SuperVO[] filterWithRight(SuperVO[] vos) {
		//如果不是查看，则无权
		if(rightConfig.getActionId() != RightActionEnum.VIEW.getValue()) {
			vos = new SuperVO[0];
		}
		return vos;
	}

	@Override
	public String getRightSql() {
		RightObject rightObj = rightConfig.getRightObject();
		String relIdField = rightObj.getRelDataIdField();
		String relUserField = rightObj.getRelUserField();
		String relUserTableName = rightObj.getRelUserTableName();
		String idField = rightObj.getPkField();
		String where = idField + " in (select " + relIdField + " from "
				+ relUserTableName + " where " + relUserField + " = " + curUser
				+ ")";
		return where;
	}
}

